<?php

namespace Admin\Controller;
use Think\Controller;
class GoodattributeController extends CommonController
{

	/**
	 * @var      int
	 */
	public $goodAttributeid;
	/**
	 * @var      String
	 */
	public $attributeName;
	/**
	 * @var      String
	 */
	public $attributeDefVal;
	/**
	 * @var      int
	 */
	public $goodcategoryId;
	/**
	 * @return   void
	 */


	/**
	 * @return   void
	 */
	public function checkGoodAttribute()
	{
		// TODO: implement

		//获得请求参数
		$goodcategoryId = I('get.GoodcategoryId',0,'int');
		//获得分类列表
		$data['goodcategory'] = D('goodcategory')->getList();
		//默认选中全部分类
		$where="goodattribute.GoodcategoryId=$goodcategoryId";
		if($goodcategoryId==0){
			unset($where);
		}
		//获得属性信息
		$data['goodattribute'] = M('goodattribute')->join("join goodcategory ON goodcategory.GoodcategoryId	= goodattribute.GoodcategoryId")->where($where)->select();
		//视图
		$data['GoodcategoryName']=M('goodcategory')->field('CategoryName')->where()->find();
		$data['GoodcategoryId'] = $goodcategoryId;
		$this->assign($data);
		$this->display('index');

	}

	public function addGoodAttribute()
	{
		// TODO: implement
		//接收请求参数
		$goodcategoryId = I('get.GoodcategoryId',0,'int');
		if($goodcategoryId<0){
			$this->error('请先添加分类',U('Goodcategory/add'),0);
		}
		//处理表单
		if(IS_POST){
			if(I('post.AttributeName')==''||I('post.AttributeDefVal')==''){
				echo "<script language='javascript' type='text/javascript'>alert('属性名称和属性值不可为空！')</script>";
				//				$url = history.go(-1);		//返回上一页（表单提交后返回，带参数）
				echo "<script language='javascript' type='text/javascript'>history.back();</script>";
				return false;
			}
			$rst = $this->create('goodattribute','add',1);
			if($rst===false){
				$this->error('添加失败');
			}
			header('location:'.__CONTROLLER__.'/checkGoodAttribute/GoodcategoryId/'.I('post.GoodcategoryId'));
			return;
		}
		$data['goodcategory'] = D('goodcategory')->getList();
		$data['GoodcategoryId'] = $goodcategoryId;
		$this->assign($data);
		$this->display('add');
	}

	/**
	 * @return   void
	 */
	public function deleteGoodAttribute()
	{
		// TODO: implement
		$id = I('get.GoodAttributeid',0,'int');
		M('goodattribute')->where("GoodAttributeid=$id")->delete();
		//删除商品属性表中的关联属性值
		M('goods_has_goodattribute')->where("goodattribute_GoodAttributeid=$id")->delete();
		//成功跳转
		$goodcategoryId = I('get.GoodcategoryId',0,'int');
		$url = __CONTROLLER__."/index/GoodcategoryId/".$goodcategoryId;
		echo "<script language='javascript' type='text/javascript'>window.location.href='$url'</script>";

	}

	/**
	 * @return   void
	 */
	public function modifyGoodAttribute()
	{
		// TODO: implement
		$id = I('get.GoodAttributeid',0,'int');
		if(IS_POST){
			$rst = $this->create('goodattribute','save',2,"GoodAttributeid=$id");
			if($rst===false){
				$this->error('修改失败',1);
			}
			$goodcategoryId = I('get.GoodcategoryId',0,'int');
			$this->success('修改成功',U('checkGoodAttribute',"GoodcategoryId=$goodcategoryId"));
			return;
		}
		$data = M('goodattribute')->where("GoodAttributeid=$id")->find();
		$data['goodcategory']=D('goodcategory')->getList();
		$this->assign($data);
		$this->display('update');
	}

	public function delSelect(){
		$ids = implode(",",I('post.attributeids'));
		$where['GoodAttributeid'] = array('in', $ids);
		$res = M('goodattribute')->where($where)->delete();
		if($res === false){
			$this->error("删除失败");
		}
		header('location:'.__CONTROLLER__.'/checkGoodAttribute/GoodcategoryId/'.I('post.GoodcategoryId'));
	}


	public function checkattrajax(){
		if(IS_POST){
			//			$rst=M('goodcategory')->data(I('post.'))->add();  // 如果主键是自动增长型 成功后返回值就是最新插入的值，返回id

			//默认选中第一个分类
			$goodcategoryId = I('post.GoodcategoryId',0,'int');
			$where="GoodcategoryId=$goodcategoryId";
			if($goodcategoryId==0){
				unset($where);
			}
			//获得属性信息
			$data['goodattribute'] = M('goodattribute')->where($where)->select();

			$this->ajaxReturn($data['goodattribute']);	//返回到ajax中是msg
		}
	}


}

?>